import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Mouse3 extends Applet
   implements MouseListener, MouseMotionListener {

   int width, height;
   Vector listOfPositions;
   ArrayList<Integer> tabx; 
   double Wx=0;
   double Wy=0;

   public void init() {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );

      listOfPositions = new Vector();
      tabx = new ArrayList<Integer>();

      addMouseListener( this );
      addMouseMotionListener( this );
   }

   public void mouseEntered( MouseEvent e ) { }
   public void mouseExited( MouseEvent e ) { }
   public void mouseClicked( MouseEvent e ) { }
   public void mousePressed( MouseEvent e ) {}
   public void mouseReleased( MouseEvent e ) {}
   public void mouseMoved( MouseEvent e ) {}
   public void mouseDragged( MouseEvent e ) { 
      // dodaj nową pozycję do końca listy
      listOfPositions.addElement( new Point( e.getX(), e.getY() ) );
      if(e.getY()==250)
      {
          tabx.add(e.getX());
      }

      repaint();
      e.consume();
    }

   public void paint( Graphics g ) {
      g.setColor( Color.white );
      //moje
        int i=0;
        g.drawLine(0,250,500,250);              //os x
        g.drawLine(493, 247, 500, 250);  //strzeleczka
        g.drawLine(493, 253, 500, 250);  //
        g.drawLine(250,0,250,500);              //os y
        g.drawLine(247, 5, 250, 0);      // strzaleczka
        g.drawLine(253, 5, 250, 0);
        while(i<500)
        {
           i+=10;
           g.drawLine(i,245,i,255); 
           g.drawLine(245,i,255,i);
//            if (i>240)
//            {
//            String s = Integer.toString((i-250)/10);
//            g.drawString(s, i, 240);
//           }
        }
        //moje
        for ( int j = 1; j < listOfPositions.size(); ++j ) {
         Point A = (Point)(listOfPositions.elementAt(j-1));
         Point B = (Point)(listOfPositions.elementAt(j));
         g.drawLine( A.x, A.y, B.x, B.y );
        }
      //moje
      if(tabx.size() == 2){
          Object obj1 = Collections.min(tabx); Integer min  =(Integer) obj1;
          Object obj2 = Collections.max(tabx); Integer max  =(Integer) obj2;
          Wx=((max-min)/2)+min;
          g.drawLine((int)Wx,0,(int)Wx,500);
        //   g.drawString("Wx = "+Wx,30,60);   
           for(int jj=0;jj<=listOfPositions.size();jj++)
           {
              Object k =listOfPositions.get(jj);
              Point eo  =(Point) k;
               if(eo.getX()>min  && eo.getX()<max && eo.getX()==Wx)
               {
                 Wy= eo.getY();
               }
           }
           //g.drawString("Wy = "+Wy,30,50);
          for(int h=0;h<=tabx.size();h++)
          {
              int j =40*h;
             g.drawString("x"+h+" ="+tabx.get(h),j,80);
             Object k =listOfPositions.get(1);
             Point e=(Point) k;
             g.drawString("poz ="+ e.getX() ,120,120); 
          }
        g.drawString("Wx = "+(int)Wx,30,60);
      g.drawString("Wy = "+(int)Wy,30,100); 
        }  
    
    g.drawString("Wykres funkcji opisany funkcją = y=a(x-"+(int)Wx+")^2+"+(int)Wy,30,100);
   
    }
}